{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "句子“ 电脑是20世纪人类最伟大的发明之一 ”是以“电脑”开头的\n",
      "将'电脑'替换为'计算机'后的结果： 计算机是20世纪人类最伟大的发明之一\n",
      "句子“ 按性能的不同，电脑可分为巨型机，大型机，小型机，工作站和微型电脑等 ”不是以“电脑”开头的\n",
      "将'电脑'替换为'计算机'后的结果： 按性能的不同，计算机可分为巨型机，大型机，小型机，工作站和微型计算机等\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "text='电脑是20世纪人类最伟大的发明之一。按性能的不同，电脑可分为巨型机，大型机，小型机，工作站和微型电脑等。'\n",
    "p_string=text.split('。')\n",
    "for line in p_string:\n",
    "    #判断是否以“电脑”开头\n",
    "    if re.match('电脑',line) is not None:\n",
    "        print(\"句子“\",line,\"”是以“电脑”开头的\")\n",
    "    elif line:\n",
    "        print(\"句子“\",line,\"”不是以“电脑”开头的\")\n",
    "    #将text中的“电脑”替换为“计算机”\n",
    "    if re.search('电脑',line)is not None:\n",
    "        line=re.sub('电脑','计算机',line)\n",
    "        print(\"将'电脑'替换为'计算机'后的结果：\",line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
